home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DOS.SWG / 0048_Sharing Files.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-03  |  4KB  |  103 lines

  1.  
  2. {
  3.  
  4.    I've been puzzling over Share myself the last week. Here's some
  5.    tips from what I've found:
  6.  
  7.    1. Remember, if you are trying to write to the same region from
  8.    both processes, you will *still* get a sharing violation with
  9.    access denied to the last one to ask ! There is a subfunc, (440Bh
  10.    I think) for changing the default number of tries that share will
  11.    retry for access. You might want to look into that. Otherwise you
  12.    could use something like the OpenTxtFile routine below, modified
  13.    for use on non-text files, or both. (You aren't trying to share
  14.    text files are you? If so, there IS a way to do it, let me know).
  15.  
  16.    2. Also note that you set the filemode AFTER assignment, and
  17.    BEFORE a reset, rewrite or append.
  18.  
  19.    3. The following are 2 functions I've put together to handle my
  20.    stuff. Note that the first is for non-text files, the second is
  21.    for text files. The text file routine uses an external TFDD unit
  22.    to set up the filemode variable so it works with text files.
  23.    Holler if you want the unit also.........
  24.  
  25.            (* Call this to lock or unlock the ENTIRE file
  26. ****** use lock =$00 & unlock = $01 constants for action *********
  27.               ***** SHARE.EXE MUST be loaded ! *******
  28.              Do NOT use on Text Files ! will NOT work !
  29. You could modify this to only lock certain regions by passing values
  30. for a start and stop region. Load CX/DX and DI/SI as done below. *)
  31. }
  32.  
  33. Function LockFile(var f; action:byte):boolean;
  34. Var
  35.    fsize : longint;
  36. Begin
  37.  if GotShare then                         (* Share loaded ? *)
  38.    begin
  39.      fsize := longint(filesize(file(f)));   (* Get filesize *)
  40.      Regs.AH := $5C;                             (* Subfunc *)
  41.      Regs.AL := Action;           (* $00=Lock or $01=unlock *)
  42.      Regs.BX := FileRec(f).Handle;        (* Git the handle *)
  43.      Regs.CX := Hi($00);                   (* Start of file *)
  44.      Regs.DX := Lo($00);
  45.      Regs.DI := Lo(fsize);           (* Compute end of file *)
  46.      Regs.SI := Hi(fsize);
  47.      Intr($21, Regs);
  48.      if ((Regs.FLAGS and $01) = 0) then LockFile := true
  49.      else
  50.        begin
  51.          IORes := regs.AX;      (* If fails, errcode is in AX *)
  52.          LockFile := false;    (* IORes is a global that gets *)
  53.        end;                   (* used in IOReport if an error *)
  54.    end;
  55. End;
  56.  
  57. (*-------------------------------------------------------------*)
  58.   (* Share compatable  Will retry if access denied, tries times
  59.            5 Tries is equivilent to a 1/2 second wait
  60.  
  61.                                   ----- Sharing Method -----
  62.  Access         Compatibility   Deny   Deny    Deny   Deny
  63.  Method            Mode         Both   Write   Read   None
  64.  ---------------------------------------------------------
  65.  Read Only           0           16     32      48     64
  66.  Write Only          1           17     33      49     65
  67.  Read/Write          2*          18     34      50     66
  68.                         * = default                        *)
  69.  
  70. FUNCTION OpenTxtFile(var f; fname:string; tries:word):boolean;
  71. VAR
  72.    i  : word;
  73. Begin
  74.   i := 0;
  75.  if GotShare then                       (* Share loaded ? *)
  76.    begin
  77.      AssignText(text(f),Fname);         (* From TxtShare unit *)
  78.      FileMode := 34;            (* Open in r/w-deny write mode *)
  79.    end
  80.  else  Assign(text(f),Fname);
  81.  Repeat
  82.   {$I-} Reset(text(f));
  83.   IORes := IoResult; {$I+}
  84.   if IORes = 5 then              (* Only repeat if denied access *)
  85.     begin
  86.       wait(100);                (* Wait 1/10 second before retry *)
  87.       INC(i);                 (* Use your own delay routine here *)
  88.     end
  89.   else i := tries;                (*  Quit if not a sharing deny *)
  90.  Until (IORes = 0) OR (i >= tries);
  91.  if GotShare then FileMode := 2;      (* Set FileMode to default *)
  92.  OpenTxtFile := IORes = 0;
  93. End;
  94.  
  95. {    ****** Here's a quick SHARE detect routine ********* }
  96.  
  97. Function ShareInstalled : boolean; assembler;
  98. asm
  99.   mov ax,$1000
  100.   int $2f
  101. end;
  102.  
  103.